package de.bib.blue.manage.finance.util;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

public class UIHelper {

	/**
	 * Buat separator sesuai style tersebut.
	 * 
	 * @param parent
	 *            composite harus berlayout {@link GridLayout}
	 * @param style
	 *            harus {@link SWT#HORIZONTAL} atau {@link SWT#VERTICAL}
	 * @return label separator
	 */
	public static Label separator(Composite parent, int style) {
		Label separator = new Label(parent, SWT.SEPARATOR | style);
		if (style == SWT.HORIZONTAL) {
			separator.setLayoutData(fillHorizontal());
		} else if (style == SWT.VERTICAL) {
			separator.setLayoutData(fillVertical());
		}
		return separator;
	}

	/**
	 * Buat grid data yang fill kedua arah.
	 * 
	 * @return grid data
	 */
	public static GridData fillBoth() {
		return new GridData(GridData.FILL_BOTH);
	}

	/**
	 * Buat grid data yang fill horizontal.
	 * 
	 * @return grid data
	 */
	public static GridData fillHorizontal() {
		return new GridData(SWT.FILL, SWT.CENTER, true, false);
	}

	/**
	 * Buat grid data yang fill vertical.
	 * 
	 * @return grid data
	 */
	public static GridData fillVertical() {
		return new GridData(SWT.CENTER, SWT.FILL, false, true);
	}

	/**
	 * Buat grid satu kolom layout dengan space dan margin.
	 * 
	 * @return group layout tanpa space dan margin
	 */
	public static GridLayout createNoMarginSpaceGridLayout() {
		GridLayout layout = new GridLayout(1, false);
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		return layout;
	}

	/**
	 * Buat kolom untuk tabel.
	 * 
	 * @param table
	 *            tabel
	 * @param label
	 *            label/nama kolom
	 * @param width
	 *            lebar kolom dalam piksel
	 */
	public static void createColumn(Table table, String label, int width) {
		TableColumn column = new TableColumn(table, SWT.None);
		column.setText(label);
		column.setResizable(true);
		column.setWidth(width);
	}

	/**
	 * Buat teks.
	 * 
	 * @param parent
	 *            composite induknya
	 * @return {@link Text}
	 */
	public static Text createText(Composite parent) {
		Text text = new Text(parent, SWT.BORDER);
		text.setLayoutData(UIHelper.fillHorizontal());
		return text;
	}

	/**
	 * Buat label.
	 * 
	 * @param parent
	 *            composite induk
	 * @param text
	 *            label
	 * @return {@link Label}
	 */
	public static Label createLabel(Composite parent, String text) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		return label;
	}
}
